package com.itheima.a02udpdemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class ReceiveMessageDemo {
    public static void main(String[] args) throws IOException {
        //1.创建接收数据的对象
        DatagramSocket ds = new DatagramSocket(10086);


        //2.创建数据包用于存储接收到的数据
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
        ds.receive(dp);

        //3 解析数据包中的数据
        byte[] data = dp.getData();
        InetAddress address = dp.getAddress();
        int port = dp.getPort();
        System.out.println("接收到的数据为："+new String(data));
        System.out.println("接收到的数据是从"+address+"这台电脑中的"+port+"端口号发出的");

        ds.close();
    }
}
